package com.cn.web.controller.ComponentMechanical;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.SingleRegistrationTableDto;
import com.cn.bean.pojo.ComponentMechanical.AbnormalApplicationMainTable;
import com.cn.bean.pojo.ComponentMechanical.AbnormalApplicationSubTable;
import com.cn.bean.pojo.lbj.*;
import com.cn.bean.pojo.workOrder.BusinessItemWorkOrderPool;
import com.cn.bean.pojo.workOrder.WorkOrderPlanMainTable;
import com.cn.bean.pojo.workOrder.WorkOrderPlanSubTable;
import com.cn.config.config.Result;
import com.cn.service.server.ComponentMechanical.AbnormalApplicationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Abn")
@Transactional
public class AbnormalApplicationController {
    @Autowired
    private AbnormalApplicationService abnormalApplicationService;
    @RequestMapping("/select/AbnormalApplicationMain")
    @ResponseBody
    public List<AbnormalApplicationMainTable> selectAbnormalApplicationMain(@RequestBody AbnormalApplicationMainTable abnormalApplicationMainTable) {
        List<AbnormalApplicationMainTable> assists = abnormalApplicationService.selectAbnormalApplicationMain(abnormalApplicationMainTable);
        return assists;
    }
    @RequestMapping("/select/AbnormalSubTable")
    @ResponseBody
    public List<AbnormalApplicationSubTable> selectAbnormalApplicationSub(@RequestBody AbnormalApplicationSubTable abnormalApplicationSubTable) {
        List<AbnormalApplicationSubTable> assists = abnormalApplicationService.selectAbnormalSubTable(abnormalApplicationSubTable);
        return assists;
    }
    @RequestMapping("/select/CurrentData")
    @ResponseBody
    public List<SingleParameterItemCurrentDataTable> selectCurrentDataTable(@RequestBody SingleParameterItemCurrentDataTable singleParameterItemCurrentDataTable) {
        List<SingleParameterItemCurrentDataTable> sub = abnormalApplicationService.selectCurrentData(singleParameterItemCurrentDataTable);
        return sub;
    }
    @RequestMapping("/insert/AbnormalApplication")
    @ResponseBody
    public Result insertAbnormalApplication(@RequestBody JSONObject jsonObject)  {
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        AbnormalApplicationMainTable mainTable = new AbnormalApplicationMainTable();
        Integer id = abnormalApplicationService.getAbnormalApplicationMainAutoid();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        id = id != null ? ++id : 1;
        String userName = jsonObject.getString("userName");
        Integer exceptionMode=jsonObject.getInteger("exceptionMode");
        String abnormalCause=jsonObject.getString("abnormalCause");
        mainTable.setId(id);
        mainTable.setApplicant(userName);
        mainTable.setApplicationTime(format.format(date));
        mainTable.setExceptionMode(exceptionMode);
        mainTable.setAbnormalCause(abnormalCause);
        mainTable.setDjCode(id);
        int a = abnormalApplicationService.insertAbnormalApplicationMain(mainTable);
        if (a != 1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<AbnormalApplicationSubTable> djBhList = dataArray.toJavaList(AbnormalApplicationSubTable.class);
            for (AbnormalApplicationSubTable dataStructureDto : djBhList) {
                AbnormalApplicationSubTable subTable = new AbnormalApplicationSubTable();
                ComponentSingleRegistrationTable componentSingleTable=new ComponentSingleRegistrationTable();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = abnormalApplicationService.getAbnormalApplicationSubAutoid();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                int b = abnormalApplicationService.insertAbnormalApplicationSub(subTable);
                if (exceptionMode==4){
                    int c=abnormalApplicationService.updateAbnormal(subTable.getUniqueCode());
                }
                componentSingleTable.setMaterialStatus(exceptionMode);
                componentSingleTable.setUniqueCode(subTable.getUniqueCode());
                componentSingleTable.setLockTable("异常申请表");
                int c=abnormalApplicationService.updateAbnormalStatus(componentSingleTable);
               int d= abnormalApplicationService.lockAbnormal(componentSingleTable);
                if (b != 1&&c != 1&&d != 1) {
                    return Result.failure("500");
                }
            }
        }
        return Result.success(200);
    }
    @RequestMapping("/update/AbnormalApplication")
    @ResponseBody
    public Result updateAbnormalApplication(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        AbnormalApplicationMainTable mainTable = new AbnormalApplicationMainTable();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int id = jsonObject.getInteger("id");
        String userName = jsonObject.getString("userName");
        Integer exceptionMode=jsonObject.getInteger("exceptionMode");
        String abnormalCause=jsonObject.getString("abnormalCause");
        mainTable.setId(id);
        mainTable.setApplicant(userName);
        mainTable.setApplicationTime(format.format(date));
        mainTable.setExceptionMode(exceptionMode);
        mainTable.setAbnormalCause(abnormalCause);
        mainTable.setDjCode(id);
        int a = abnormalApplicationService.updateAbnormalData(mainTable);
        if (a != 1) {
            return Result.failure("500","更新主表失败");
        }
        List<AbnormalApplicationSubTable> statusOperationDtos=abnormalApplicationService.selectAbnormalApplicationSubTable(id);
        if (statusOperationDtos.size()>0){
            int sub = abnormalApplicationService.delAbnormalSub(id);
        }
        if (dataArray==null||dataArray.isEmpty()){
            return Result.failure("500");
        }
        List<AbnormalApplicationSubTable> djBhList = dataArray.toJavaList(AbnormalApplicationSubTable.class);
        for (AbnormalApplicationSubTable dataStructureDto : djBhList) {
            AbnormalApplicationSubTable subTable = new AbnormalApplicationSubTable();
            ComponentSingleRegistrationTable componentSingleTable=new ComponentSingleRegistrationTable();
            BeanUtils.copyProperties(dataStructureDto, subTable);
            Integer autoid = abnormalApplicationService.getAbnormalApplicationSubAutoid();
            autoid = autoid != null ? ++autoid : 1;
            subTable.setId(autoid);
            subTable.setMid(id);
            int b = abnormalApplicationService.insertAbnormalApplicationSub(subTable);
            if (exceptionMode==4){
                int c=abnormalApplicationService.updateAbnormal(subTable.getUniqueCode());
            }
            componentSingleTable.setMaterialStatus(exceptionMode);
            componentSingleTable.setUniqueCode(subTable.getUniqueCode());
            int c=abnormalApplicationService.updateAbnormalStatus(componentSingleTable);
            if (b != 1&&c != 1) {
                return Result.failure("500");
            }
        }
        return Result.success(200);
    }
    @RequestMapping("/audit/AbnormalApplication")
    @ResponseBody
    public Result AbnormalApplicationAudit(@RequestBody JSONObject jsonObject){
        JSONArray djBhArray = jsonObject.getJSONArray("id");
        String userName = jsonObject.getString("userName");
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (djBhArray != null && !djBhArray.isEmpty()) {
            List<Integer> djBhList = djBhArray.toJavaList(Integer.class);
            for (Integer id : djBhList) {
                abnormalApplicationService.updateAbnormalAudit(id);
            }
            return Result.success(200);
        }
        return Result.failure("500", "审核失败");
    }
}
